package com.composite.entity;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Employee {

    private String name;
    private String dept;
    private int salary;
    private List<Employee> subordinates;

    //构造函数
    public Employee(String name, String dept, int sal) {
        this.name = name;
        this.dept = dept;
        this.salary = sal;
        subordinates = new ArrayList<Employee>();
    }

    public void add(Employee e) {
        subordinates.add(e);
    }

    public void remove(Employee e) {
        subordinates.remove(e);
    }

    public List<Employee> getSubordinates(){
        return subordinates;
    }

    /*
        重写equals及hashcode。此处认为name、dept和salary相等就属于同一个对象
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return salary == employee.salary && name.equals(employee.name) && dept.equals(employee.dept);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, dept, salary);
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", dept='" + dept + '\'' +
                ", salary=" + salary +
                // ", subordinates=" + subordinates +
                '}';
    }

    public String toTreeShow() {
        return new StringBuilder()
                .append(name)
                .append("(")
                .append(dept)
                .append(",")
                .append(salary)
                .append(")")
                .toString();
    }
}
